package nia;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannelCopy {
    public static void main(String[] args) throws IOException {
        File rf = new File("c:\\users\\xianf\\desktop\\aa.txt");
        File wf = new File("c:\\users\\xianf\\desktop\\bb.txt");
        FileInputStream fileInputStream = new FileInputStream(rf);
        FileOutputStream fileOutputStream = new FileOutputStream(wf);

        FileChannel channelIn = fileInputStream.getChannel();
        FileChannel channelOut = fileOutputStream.getChannel();

        ByteBuffer bb = ByteBuffer.allocate(10);


        while (true) {
            /*
            position = 0;
            limit = capacity;
            mark = -1;
             */
            bb.clear();
            int read = channelIn.read(bb);
            System.out.println(read);
            if (read == -1) {
                break;
            }

            /*
            limit = position;
            position = 0;
            mark = -1;
             */
            bb.flip();

            channelOut.write(bb);

        }
    }
}
